<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
class doc_categories extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->lang->load('doc_cate');
        $this->load->model("doc_categories_model");
    }

    public function sort($field = "created", $order = "desc")
    {
        $this->doc_categories_model->dataSort($field, $order);
        $this->index();
    }

    public function index($target = 0)
    {
        $arr = $this->doc_categories_model->dataIndex($target);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » <span>' . t("doc_categories") . '</span>';
        $data = array("content" => $this->load->view("doc_categories/main", $arr, true), "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function find()
    {
        if (!isset($_POST["txt_find"]))
            return redirect(site_url("doc_categories/index"));
        if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset")
            $this->session->unset_userdata("sm_find_doc_categories");
        else {
            $keyword = $this->input->post("txt_find", true);
            if ($keyword != "") {
                notify_set_messages(notify_info(t("Result") . " " . $keyword), "doc_categories");
                $this->session->set_userdata("sm_find_doc_categories", $keyword);
            }
        }

        redirect(site_url("doc_categories/index"));
    }
    public function filterdoc_menus()
    {
        $id = $_POST["mid"];
        if (isset($_POST["btnReset"]))
            $this->session->unset_userdata("sm_filter_doc_menus");
        else
            $this->session->set_userdata("sm_filter_doc_menus", $id);
        redirect(site_url("doc_categories"));
    }
    public function add()
    {
        $arr = $this->doc_categories_model->dataAdd();
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
        <a href="/admin/doc_categories">' . t("doc_categories") . '</a> » <span>' . t("Add") . '</span>';
        $data = array("content" => $this->load->view("doc_categories/add", $arr, true), "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function edit($id)
    {
        $arr = $this->doc_categories_model->dataEdit($id);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
        <a href="/admin/doc_categories">' . t("doc_categories") . '</a> » <span>' . t("Edit") . '</span>';
        $data = array("content" => $this->load->view("doc_categories/edit", $arr, true), "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function doAdd()
    {
        if (!$this->doc_categories_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "doc_categories");
            return $this->add();
        }
        if ($this->doc_categories_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is added")), "doc_categories");
            return redirect(site_url("doc_categories"));
        }

    }

    public function doEdit()
    {
        if (!$this->doc_categories_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "doc_categories");
            return $this->edit(decrypt($_POST["idx"]));
        }
        if ($this->doc_categories_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is updated")), "doc_categories");
            redirect(site_url("doc_categories"));
        }

    }

    public function del($id)
    {
        if (!$this->doc_categories_model->docMenuDel($id)) {
            notify_set_messages(notify_atten('Tồn tại tài liệu, danh mục không được phép xóa!'), "doc_categories");
        } else {
            notify_set_messages(notify_success(t("Danh mục đã được xóa")), "doc_categories");
        }
        redirect(site_url("doc_categories"));
    }

}
